
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>

<?php 
if($_POST){
	$name = strip_tags($_POST['nickname']);
	$psw = $_POST['password'];
	$briagePsw = md5($psw."a37b621cb27c2557ee52a681");
	$tbriagePsw = md5($briagePsw."a37b621cb27c2557ee52a681");
	$cpassword = md5($tbriagePsw."bc860d113ea4310a9b268e3");



$db = include('../Application/Common/Conf/config.php');  

$host =  $db['DB_HOST'];
$port =  $db['DB_PORT'];
$dbname =  $db['DB_NAME'];
$user =  $db['DB_USER'];
$psw =  $db['DB_PWD'];
$dbconn = pg_connect("host=$host port=$port dbname=$dbname user=$user password=$psw");
		 if($dbconn)
		 {
			$Tsql = "select nuserid from tbuseridpool;";
			$R = pg_query($dbconn,$Tsql);
			$total = pg_fetch_all($R);						
			if($total){
			foreach($total as $val){
				$userIds[] = $val['nuserid'];	
			}			
			if($userIds){
				 $key = rand(0,count($userIds)-1);
				 $id = $userIds[$key];	 
				 $regisuccessUrl = "./regsuccess.php?id=".$id."&pw=".$briagePsw;
				 $Di = "INSERT INTO tbuseridalloc (nuserid,nstartid,ncount) VALUES ($id,$id,1);";  //保存tbuseridalloc表
				 $Ii =  "INSERT INTO tbuserinformation (nuserid,cpassword,calias) VALUES ('$id','$cpassword','$name');";  //保存tbuseridalloc表
				 $Ci =  "INSERT INTO tbuseraccount (nuserid) VALUES ($id);"; //保存tbuseraccount表
				 $d =  "delete from tbuseridpool where nuserid=$id;";
				$flag = true;
				if(!pg_query($dbconn,$Di)){
					$flag = false;				
				}
				if(!pg_query($dbconn,$Ii)){
					$flag = false;				
				}
				if(!pg_query($dbconn,$Ci)){
				$flag = false;		
				}				
				if($flag){				
					if(pg_query($dbconn,$d)){
						echo "<script language='javascript' 
						type='text/javascript'>";  
						echo "window.location.href='$regisuccessUrl'";  
						echo "</script>";      					
					}
				}else{
				  echo "注册失败";
				}		
			}			
			}else{
				echo "帐号已发放完毕";			
			}
		}

}
?>

</body>
</html>